package com.ryf.maven.servlet.student;

import com.google.gson.Gson;
import com.ryf.maven.dao.StudentDao;
import com.ryf.maven.entity.BaseResponse;
import com.ryf.maven.entity.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Description
 * @ClassName StudentSearchServlet
 * @Author L-Cohen
 * @date 2020.12.05 15:34
 */
@WebServlet(urlPatterns = "/stu/search")
public class StudentSearchServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        BaseResponse<Student> response = new BaseResponse<Student>();
        String stuNo = req.getParameter("stuNo");

        Student student = StudentDao.getStuByStuNo(stuNo);

        if (student != null){
            response.setCode(200);
            response.setMsg("查询成功");
        }
        else {
            response.setCode(600);
            response.setMsg("查询失败");
        }

        response.setData(student);


        Gson gson = new Gson();
        String json = gson.toJson(response);
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print(json);
        out.flush();
    }
}
